package com.tfswx.jcyw.print.config;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.nio.file.Files;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 使用文件进行实时存储的Map
 *
 * @author 张述江
 * @date 2023/4/24
 */
@Slf4j
public class FileMap<T> implements Map<String, T> {
    private final Map<String, T> map = new ConcurrentHashMap<>();
    private final File file;

    public FileMap(String filePath) {
        this(new File(filePath));
    }

    @SneakyThrows
    public FileMap(File file) {
        this.file = file.getCanonicalFile();
        File parentFile = file.getParentFile();
        if (!parentFile.exists() && !parentFile.mkdirs()) {
            throw new RuntimeException("文件夹【" + parentFile.getCanonicalPath() + "】创建失败");
        }
        if (file.exists()) {
            read();
        }
    }

    @Override
    public int size() {
        return map.size();
    }

    @Override
    public boolean isEmpty() {
        return map.isEmpty();
    }

    @Override
    public boolean containsKey(Object key) {
        return map.containsKey(key);
    }

    @Override
    public boolean containsValue(Object value) {
        return map.containsValue(value);
    }

    @Override
    public T get(Object key) {
        return map.get(key);
    }

    @Override
    public T put(String key, T value) {
        if (!(value instanceof Serializable)) {
            throw new RuntimeException("值不为可序列化对象");
        }
        T put = map.put(key, value);
        save();
        return put;
    }

    @Override
    public T remove(Object key) {
        T remove = map.remove(key);
        save();
        return remove;
    }

    @Override
    public void putAll(Map<? extends String, ? extends T> m) {
        boolean isOk = m.values().stream().allMatch(value -> (value instanceof Serializable));
        if (!isOk) {
            throw new RuntimeException("值不为可序列化对象");
        }
        map.putAll(m);
        save();
    }

    @Override
    public void clear() {
        map.clear();
        save();
    }

    @Override
    public Set<String> keySet() {
        return map.keySet();
    }

    @Override
    public Collection<T> values() {
        return map.values();
    }

    @Override
    public Set<Entry<String, T>> entrySet() {
        return map.entrySet();
    }


    private void read() {
        try (ObjectInputStream objectInputStream = new ObjectInputStream(Files.newInputStream(file.toPath()))) {
            Map<String, T> o = (Map<String, T>) objectInputStream.readObject();
            map.putAll(o);
        } catch (Exception e) {
            log.warn("持久化token读取错误");
        }
    }

    @SneakyThrows
    private void save() {
        try (ObjectOutputStream objectOutputStream = new ObjectOutputStream(Files.newOutputStream(file.toPath()))) {
            objectOutputStream.writeObject(map);
        }
    }
}
